<template>
	<view class="home">
		<swiper indicator-dots circular autoplay interval=3000>
			<swiper-item v-for="item in swiper" :key="item.id">
				<image :src="item.img"></image>
			</swiper-item>
		</swiper>
		<view class="nav">
			<view class="nav_item" v-for="(item,index) in navs" :key="index" @click="navItemClick(item.path)">
				<view :class="item.icons"></view>
				<text>{{item.title}}</text>
			</view>
		</view>
		<view class="hot_goods">
			<view class="tit">推荐商品</view>
			<goods-list :goods="goods" @goodsItemClick="showGoods"></goods-list>
		</view>
	</view>
</template>

<script>
	import goodsList from "../../components/goods-list/goods-list.vue"
	export default {
		data() {
			return {
				swiper: [],
				goods: [],
				navs: [{
						icons: "iconfont icon-ziyuan",
						title: "黑马超市",
						path: "/pages/goods/goods"
					},
					{
						icons: "iconfont icon-tupian",
						title: "社区图片",
						path: "/pages/pics/pics"
					},
					{
						icons: "iconfont icon-guanyuwomen",
						title: "联系我们",
						path: "/pages/contact/contact"
					},
					{
						icons: "iconfont icon-shipin",
						title: "学习视频",
						path: "/pages/videos/videos"
					}
				]
			}
		},
		onLoad() { //页面的生命周期 执行一次
			this.getSwiper();
			this.getHotGoods();
		},
		components: {
			"goods-list": goodsList
		},
		methods: {
			showGoods(id){
				uni.navigateTo({
					url:'../goods-detail/goods-detail?id='+id
				})
			},
			async getSwiper() {
				const res = await this.$myRequest({
					url: '/api/getlunbo'
				})
				this.swiper = res.data.message;
			},
			// 获取热门商品列表数据
			async getHotGoods() {
				const res = await this.$myRequest({
					url: '/api/getgoods?pageindex=1'
				})
				this.goods = res.data.message;
			},
			navItemClick(url) {
				console.log(url)
				uni.navigateTo({
					url
				})
			},
		},
	}
</script>

<style lang="scss">
	.home {
		swiper {
			height: 380rpx;

			image {
				width: 750rpx;
				height: 380rpx;
			}
		}

		.nav {
			display: flex;
			align-items: center;

			.nav_item {
				width: 25%;
				text-align: center;

				view {
					background: $shop-color;
					line-height: 120rpx;
					width: 120rpx;
					height: 120rpx;
					border-radius: 60rpx;
					margin: 10px auto;
					line-height: 120rpx;
					color: #fff;
					font-size: 50rpx;
				}

				text {
					font-size: 30rpx;
				}
			}

			.iconfont {
				font-size: 25px;
				color: #fff;
				height: 50px;
			}

			.icon-tupian {
				font-size: 45rpx;
			}
		}

		.hot_goods {
			background: #eee;
			overflow: hidden;
			margin-top: 10px;

			.tit {
				height: 50px;
				line-height: 50px;
				color: $shop-color;
				text-align: center;
				letter-spacing: 20px;
				background: #fff;
				margin: 7rpx 0;
			}
		}
	}
</style>
